package com.shuyuansystem.controller;

import com.shuyuansystem.dao.CheckRoleMapper;
import com.shuyuansystem.dao.ProductMapper;
import com.shuyuansystem.entity.Result;
import com.shuyuansystem.pojo.Product;
import com.shuyuansystem.pojo.ReviewUpdateRequest;
import com.shuyuansystem.pojo.Reviews;
import com.shuyuansystem.pojo.User;
import com.shuyuansystem.service.ProductService;
import com.shuyuansystem.service.ReviewsService;
import com.shuyuansystem.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ReviewsController")
public class ReviewsController {
    @Autowired
    private ReviewsService reviewsService;
    @Autowired
    private CheckRoleMapper checkRoleMapper;
    @Autowired
    private ProductService productService;
    @GetMapping("/getRoleAndUserId")
    public User getRoleAndUserId(@RequestHeader("Authorization") String token) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 去除 Bearer 前缀
            token = token.replace("Bearer ", "");
            // 从 Token 中获取用户 ID
            String userId = JWTUtils.getUserIdFromToken(token);
            if (userId == null) {
                response.put("success", false);
                response.put("message", "无法从 Token 中获取用户 ID");
                return null;
            }
            // 根据用户 ID 查询角色
            String role = checkRoleMapper.checkRoleById(userId);
            if (role == null) {
                response.put("success", false);
                response.put("message", "未找到该用户的角色信息");
                return null;
            }

            User user = new User();
            user.setId(Integer.parseInt(userId));
            user.setRole(role);
            return user;
        } catch (Exception e) {
            return null;
        }
    }
    // 根据 productId 获取评论列表
    @RequestMapping("/getProductReviewsList")
    public List<Reviews> getProductReviewsList(@RequestParam("productId") Integer productId) {
        return reviewsService.getReviewsByProductId(productId);
    }
    @RequestMapping("/getProduct")
    public Product getProduct(@RequestParam("productId") String productId){
        return productService.getByProductId(productId);

    }
    @PostMapping("/addReviews")
    public Reviews addReviews(@RequestBody Reviews reviews) {
        java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
        reviews.setCreatedAt(timestamp);
        System.out.println(reviews);
        return reviewsService.insertReviews(reviews);
    }


    // 修正拼写错误，完善删除评论接口
    @DeleteMapping("/deleteReviews")
    public Result deleteReviews(@RequestParam("reviewId") Integer reviewId) {
        return reviewsService.deleteReviewById(reviewId);
    }

    // 新增修改评论接口
    // 新增修改评论接口
    @PutMapping("/updateReviews")
    public Result updateReviews(@RequestBody ReviewUpdateRequest request) {
        Integer reviewId = request.getReviewId();
        String reviewContent = request.getReviewContent();
        return reviewsService.updateReview(reviewId, reviewContent);
    }
}
